package cn.fulong.sso.client.utils;


import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * @Version 1.0
 * @Author: 云树
 * @Description:
 * @Date: 2019/2/26
 * @Modify 云树
 * @ModifyDate 2019/2/26
 * @since 1.0
 */
public class SsoUrlUtil {




    public static final String TAG_3F_QUE = "?";
    public static final String TAG_3D_EQ = "=";
    public static final String TAG_26_AND = "&";

    /**
     * 以get方式向url链接添加参数,并用params中的参数值替换url中重复的参数值
     *
     * @param url
     * @param params Map <paramsName,value>
     * @return
     */
    public static String addParams(String url, Map<String, ?> params) {
        if (StringSelfUtil.isBlank(url)) {
            return "";
        }
        url = url.trim();
        if (CollectionUtils.isEmpty(params)) {
            return url;
        }
        StringBuilder getFullUrl = new StringBuilder();
        int length = url.length();
        int index = url.indexOf(TAG_3F_QUE);
        if (index < 0) {
            //http://wwww.baidu.com
            getFullUrl.append(url).append(TAG_3F_QUE);
        } else {
            if ((length - 1) == index) {
                //http://wwww.baidu.com?
                getFullUrl.append(url);
            } else {
                //http://wwww.baidu.com?sq=xx
                //http://wwww.baidu.com?sq=xx&
                //http://wwww.baidu.com?sq=xx&age=1
                //去除重复参数
                getFullUrl.append(url.substring(0, index + 1));
                String paramsStr = url.substring(index + 1);
                String[] paramArray = paramsStr.split(TAG_26_AND);
                if (!CollectionUtils.isEmpty(paramArray)) {
                    String param;
                    String[] arr;
                    for (int i = 0, len = paramArray.length; i < len; i++) {
                        param = paramArray[i];
                        arr = param.split(TAG_3D_EQ);
                        if (!params.containsKey(arr[0])) {
                            getFullUrl.append(param).append(TAG_26_AND);
                        }
                    }
                }
            }
        }
        int size = params.size();
        for (Map.Entry<String, ?> entry : params.entrySet()) {
            getFullUrl.append(entry.getKey()).append(TAG_3D_EQ).append(entry.getValue());
            if (size > 1) {
                getFullUrl.append(TAG_26_AND);
            }
            size--;
        }
        return getFullUrl.toString();
    }


    /**
     * @param url
     * @param paramName
     * @param value
     * @return
     * @desc 添加单个url参数
     */
    public static String addParams(String url, String paramName, Object value) {
        Map<String, Object> params = new HashMap<>(2);
        params.put(paramName, value);
        return addParams(url, params);
    }

    /**
     * 移除url链接中的#{paramNames}中的参数
     *
     * @param url        String
     * @param paramNames String[]
     * @return
     */
    public static String removeParams(String url, String... paramNames) {
        if (StringSelfUtil.isBlank(url)) {
            return "";
        }
        int size = paramNames == null ? 0 : paramNames.length;
        url = url.trim();
        if (size == 0) {
            return url;
        }
        int length = url.length();
        int index = url.indexOf(TAG_3F_QUE);
        if (index < 0) {
            return url;
        }
        if ((length - 1) == index) {
            return url.substring(0, index);
        }

        String basePath = url.substring(0, index);
        String paramsStr = url.substring(index + 1);
        String[] paramArray = paramsStr.split(TAG_26_AND);
        if (CollectionUtils.isEmpty(paramArray)) {
            return url;
        }
        StringBuilder resultUrl = new StringBuilder();
        resultUrl.append(basePath).append(TAG_3F_QUE);
        HashSet<String> filterParams = new HashSet<>(Arrays.asList(paramNames));
        String key;
        String[] arr;
        for (String param : paramArray) {
            arr = param.split(TAG_3D_EQ);
            key = arr[0];
            if (!filterParams.contains(key)) {
                resultUrl.append(param).append(TAG_26_AND);
            }
        }
        int lastIndex = resultUrl.length();
        return resultUrl.delete(lastIndex - 1, lastIndex).toString();
    }



    /**
     * @param domain
     * @return 为域名添加/
     */
    public static String getRootDomain(String domain) {
        domain = domain.trim();
        return domain.endsWith("/") ? domain : domain + "/";
    }

    /**
     * @param baseUrl
     * @param suffixUrl
     * @return 合并url
     */
    public static String mergeUrl(String baseUrl, String suffixUrl) {
        String suffix = suffixUrl.trim();
        return new StringBuilder().append(getRootDomain(baseUrl)).append(suffix.startsWith("/") ? suffix.substring(1) : suffix).toString();
    }


}
